package com.eims.sls_app.utils;

import java.util.ArrayList;

import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.CommentActivity;
import com.eims.sls_app.ui.LoginActivity;
import com.eims.sls_app.view.MyToast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.TextView;
import bls.juejin.cn.R;



/**
 * 朋友圈点赞评论的popupwindow
 * 
 * @author wei.yi
 * 
 */
public class SnsPopupWindow extends PopupWindow implements OnClickListener{
	private static final String TAG = "SnsPopupWindow";
	private TextView digBtn;
	private TextView commentBtn;
	private int id;
	private boolean user;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数
	
	private MyHandler mHandler;
	private MyAsynTask mMyAsynTask;

	private Context context;
	
	// 实例化一个矩形
	private Rect mRect = new Rect();
	// 坐标的位置（x、y）
	private final int[] mLocation = new int[2];
	// 弹窗子类项选中时的监听
	private OnItemClickListener mItemClickListener;
	// 定义弹窗子类项列表
	private ArrayList<ActionItem> mActionItems = new ArrayList<ActionItem>();

	public void setmItemClickListener(OnItemClickListener mItemClickListener) {
		this.mItemClickListener = mItemClickListener;
	}
	public ArrayList<ActionItem> getmActionItems() {
		return mActionItems;
	}
	public void setmActionItems(ArrayList<ActionItem> mActionItems) {
		this.mActionItems = mActionItems;
	}


	public SnsPopupWindow(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.social_sns_popupwindow, null);
		digBtn = (TextView) view.findViewById(R.id.upBtn);
		commentBtn = (TextView) view.findViewById(R.id.commentBtn);
		digBtn.setOnClickListener(this);
		commentBtn.setOnClickListener(this);
		this.context = context;

		this.setContentView(view);
		this.setWidth(AppTools.dip2px(context, 100));
		this.setHeight(AppTools.dip2px(context, 30));
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.social_pop_anim);
		
		initItemData();
	}
	private void initItemData() {
		addAction(new ActionItem("赞"));
		addAction(new ActionItem("评论"));
	}

	public void showPopupWindow(View parent,int id){
		this.id= id;
		
		if (user) {
			digBtn.setText("已赞");
			digBtn.setEnabled(false);
		}
		parent.getLocationOnScreen(mLocation);
		// 设置矩形的大小
		mRect.set(mLocation[0], mLocation[1], mLocation[0] + parent.getWidth(),mLocation[1] + parent.getHeight());
		digBtn.setText(mActionItems.get(0).mTitle);
		if(!this.isShowing()){
			showAtLocation(parent, Gravity.NO_GRAVITY, mLocation[0] - this.getWidth()
					, mLocation[1] - ((this.getHeight() - parent.getHeight()) / 2));
		}else{
			dismiss();
		}
	}

	@Override
	public void onClick(View view) {
		dismiss();
		switch (view.getId()) {
		case R.id.upBtn://点赞
			//mItemClickListener.onItemClick(mActionItems.get(0), 0);
			//MyToast.getToast(context, id+"点赞").show();
			if (App.user == null) {
				Intent intent;
				MyToast.getToast(view.getContext(), "请先登陆").show();
				intent = new Intent(view.getContext(), LoginActivity.class);
				intent.putExtra("loginType", "bet");
				view.getContext().startActivity(intent);
			}else {
				info = id+"";
				mMyAsynTask = new MyAsynTask();
				mMyAsynTask.execute();
			}
				
			
			
			break;
		case R.id.commentBtn://评论
			//mItemClickListener.onItemClick(mActionItems.get(1), 1);
			//MyToast.getToast(context, id+"评论").show();
			if (App.user == null) {
				Intent intent;
				MyToast.getToast(view.getContext(), "请先登陆").show();
				intent = new Intent(view.getContext(), LoginActivity.class);
				intent.putExtra("loginType", "bet");
				view.getContext().startActivity(intent);
			}else {
				Intent intent = new Intent(view.getContext(), CommentActivity.class);
				intent.putExtra("id", id);
				view.getContext().startActivity(intent);
			}
			
			
			break;
		default:
			break;
		}
	}
	
	/**
	 * 添加子类项
	 */
	public void addAction(ActionItem action) {
		if (action != null) {
			mActionItems.add(action);
		}
	}
	
	/**
	 * 功能描述：弹窗子类项按钮监听事件
	 */
	public static interface OnItemClickListener {
		public void onItemClick(View view);
	}
	
class MyAsynTask extends AsyncTask<Integer, Integer, String> {
		
		
		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {
			
				opt = "87";
				time = RspBodyBaseBean.getTime();
				imei = RspBodyBaseBean.getIMEI(context);
				
				//info = RspBodyBaseBean.change_TheSun(schemeId+"", context_Sun);
				
				// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
				String key = App.key;
				crc = RspBodyBaseBean.getCrc(time, imei, key, info,
						App.user.getUid());
				auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
				String[] values = { opt, auth, info };
				Log.i(TAG, "晒单"+RequestParams.convertParams(context, opt, info));
				String result = HttpUtils.doPost(App.names, values,
						App.path);
				System.out.println("晒单：" + result);
				if (result.equals("success")) {
					error = "0";
				}
				
			
			return error;
		}

		@Override
		protected void onPostExecute(String result) {
			
			//mHandler.sendEmptyMessage(Integer.parseInt(result));
			
			super.onPostExecute(result);
		}
	}

@SuppressLint("HandlerLeak")
 class MyHandler extends Handler  {
	@Override
	public void handleMessage(Message msg) {
		
		switch (msg.what) {
		case 0:
			MyToast.getToast(context, "点赞成功").show();
			break;
		case 1:
			//MyToast.getToast(context, "重复晒单").show();
			break;
		case -2:
			
			break;
		default:
			break;
		}
		super.handleMessage(msg);
	}

	

	
}
}
